﻿<DemoPageSectionComponent Id="DataGrid-DataBinding-CustomData" ShowHorizontalScrollBar="true">
    @using DevExtreme.AspNet.Data.ResponseModel
    @using DevExtreme.AspNet.Data
    @using System.Threading
    @using System.Threading.Tasks
    @using System.Net.Http
    @using System.Text.Json

    @inject HttpClient Client

    <DxDataGrid T="@WebApiOrder" CustomData="@LoadOrderData" ShowFilterRow="true">
        <Columns>
            <DxDataGridComboBoxColumn T="@(WebApiCustomerLookup)" TextFieldName="Text" ValueFieldName="Value"
                                      CustomData="@LoadCustomerData" Field="@nameof(WebApiOrder.CustomerID)" Width="300px" />
            <DxDataGridDateEditColumn Field="@nameof(WebApiOrder.OrderDate)" Width="180px" />
            <DxDataGridSpinEditColumn Field="@nameof(WebApiOrder.Freight)" DisplayFormat="c" Width="180px" />
            <DxDataGridColumn Field="@nameof(WebApiOrder.ShipCountry)" Width="210px" />
            <DxDataGridComboBoxColumn T="@(WebApiShipperLookup)" TextFieldName="Text" ValueFieldName="Value"
                                      CustomData="@LoadShipmentData" Field="@nameof(WebApiOrder.ShipVia)" Width="220px" />
        </Columns>
    </DxDataGrid>

    @code {
        protected async Task<LoadResult> LoadOrderData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
            => await LoadCustomData("https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/Orders", options, cancellationToken);
        protected async Task<LoadResult> LoadCustomerData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
            => await LoadCustomData("https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/CustomersLookup", options, cancellationToken);
        protected async Task<LoadResult> LoadShipmentData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
            => await LoadCustomData("https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/ShippersLookup", options, cancellationToken);

        protected async Task<LoadResult> LoadCustomData(string dataEndpointUri, DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
            using var response = await Client.GetAsync(options.ConvertToGetRequestUri(dataEndpointUri), cancellationToken);
            response.EnsureSuccessStatusCode();
            using var responseStream = await response.Content.ReadAsStreamAsync();
            return await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
        }
    }
</DemoPageSectionComponent>
